﻿LocalMemoryTests = function () {
    this.Name = "Local Memory Tests";
};

LocalMemoryTests.prototype = new TestFixture();


LocalMemoryTests.prototype.TestStoreLocal = function () {
    var vm = new VM();
    vm.Execute("ldc.i4 5");
    vm.Execute("stloc 0");
    var result = vm.State.Locals.Get(0);
    this.Assert(5, result.Value);
};

LocalMemoryTests.prototype.TestLoadLocal = function () {
    var vm = new VM();
    var localValue = new StackEntry();
    localValue.Type = "i4";
    localValue.Value = 10;
    vm.State.Locals.Set(0, localValue);

    vm.Execute("ldloc 0");
    var result = vm.State.Locals.Get(0);
    this.Assert(10, result.Value);
};

LocalMemoryTests.prototype.TestMemoryRoundTrip = function () {
    var vm = new VM();

    // put a couple values on stack
    vm.Execute("ldc.i4 10");
    vm.Execute("ldc.i4 20");

    // store them to local memory
    vm.Execute("stloc 1");              // loc 1 = 20
    vm.Execute("stloc.0");              // loc 0 = 10

    // put them back on stack in reverse order
    vm.Execute("ldloc.1");              // loc 1 = 20
    vm.Execute("ldloc 0");              // loc 0 = 10

    var top = vm.State.Stack.Pop();
    this.Assert(10, top.Value);

    top = vm.State.Stack.Pop();
    this.Assert(20, top.Value);

    this.Assert(10, vm.State.Locals.Get(0).Value);
    this.Assert(20, vm.State.Locals.Get(1).Value);
};
